package org.ezplatform.integ.ocr.web.rest;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.HttpHostConnectException;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.ezplatform.core.annotation.MetaData;
import org.ezplatform.core.common.WebUser;
import org.ezplatform.core.web.util.WebUtils;
import org.ezplatform.core.web.view.OperationResult;
import org.ezplatform.integ.ocr.service.OcrWebApiService;
import org.ezplatform.system.basic.util.ConfSettingUtils;
import org.ezplatform.util.GlobalConstant;
import org.ezplatform.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

/**
 * @ClassName: OcrWebApiController
 * @Description: ocr文字识别
 * @author: hxf
 */
@MetaData("ocr集成")
@RestController
@RequestMapping(value = "/api/w/integ/ocr")
public class OcrWebApiController {
	private static final Logger LOG = LoggerFactory.getLogger(OcrWebApiController.class);

	@Autowired
	private OcrWebApiService ocrWebApiService;

	/**
	 * ocr图片转文字
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getOcrImage2Words", method = RequestMethod.GET)
	@ResponseBody
	public OperationResult getOcrImage2Words(HttpServletRequest request) {
			String convertRs = "";
			String fileUrl = ServletRequestUtils.getStringParameter(request, "fileUrl", "");
			String corpId = ServletRequestUtils.getStringParameter(request, "corpId", "0");
			String funcParams = ServletRequestUtils.getStringParameter(request, "funcParams", "");
			fileUrl = ocrWebApiService.getFullFileDownloadUrl(fileUrl);
			//从后台设置中获取服务地址
			String servicePath = ConfSettingUtils.getConfSettingValue("ocrServerPathVal", corpId);
			String appToken = ConfSettingUtils.getConfSettingValue("ocrAppTokenVal", corpId);
			servicePath +="pict/ocrImageAndRotate";
			Map<String,String> params = new HashMap<String,String>();
			params.put("fileUrl", fileUrl);
			params.put("appToken", appToken);
			
			try {
				convertRs = ocrWebApiService.sendPost_new(servicePath, params,funcParams);
			} catch (HttpHostConnectException e) {
				e.printStackTrace();
				return OperationResult.buildFailureResult("转换异常");
			} catch (ParseException e) {
				e.printStackTrace();
				return OperationResult.buildFailureResult("转换异常");
			} catch (IOException e) {
				e.printStackTrace();
				return OperationResult.buildFailureResult("转换异常");
			}
			
			JSONObject urlJson = JSONObject.parseObject(convertRs);
			if(!"200".equals(urlJson.get("code"))) {
				return OperationResult.buildFailureResult(StringUtils.null2String(urlJson.get("error")));
			}
			return OperationResult.buildSuccessResult("成功",urlJson);
	}
	
	/**
	 * 图片转pdf
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getOcrImageToPdf", method = RequestMethod.GET)
	@ResponseBody
	public OperationResult getOcrImageToPdf(HttpServletRequest request) {
			String convertRs = "";
			String fileUrl = ServletRequestUtils.getStringParameter(request, "fileUrl", "");
			String corpId = ServletRequestUtils.getStringParameter(request, "corpId", "0");
			String funcParams = ServletRequestUtils.getStringParameter(request, "funcParams", "");
			fileUrl = ocrWebApiService.getFullFileDownloadUrl(fileUrl);
			//从后台设置中获取服务地址
			String servicePath = ConfSettingUtils.getConfSettingValue("ocrServerPathVal", corpId);
			String appToken = ConfSettingUtils.getConfSettingValue("ocrAppTokenVal", corpId);
			servicePath +="pict/ocrImageToPdf";
			Map<String,String> params = new HashMap<String,String>();
			params.put("fileUrl", fileUrl);
			params.put("appToken", appToken);
			
			try {
				convertRs = ocrWebApiService.sendPost_new(servicePath, params,funcParams);
			} catch (HttpHostConnectException e) {
				e.printStackTrace();
				return OperationResult.buildFailureResult("转换异常");
			} catch (ParseException e) {
				e.printStackTrace();
				return OperationResult.buildFailureResult("转换异常");
			} catch (IOException e) {
				e.printStackTrace();
				return OperationResult.buildFailureResult("转换异常");
			}
			
			JSONObject urlJson = JSONObject.parseObject(convertRs);
			if(!"200".equals(urlJson.get("code"))) {
				return OperationResult.buildFailureResult(StringUtils.null2String(urlJson.get("error")));
			}
			return OperationResult.buildSuccessResult("成功",urlJson);
	}
	
	/**
	 * 图片转ofd
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getOcrImageToOfd", method = RequestMethod.GET)
	@ResponseBody
	public OperationResult getOcrImageToOfd(HttpServletRequest request) {
			String convertRs = "";
			String fileUrl = ServletRequestUtils.getStringParameter(request, "fileUrl", "");
			String corpId = ServletRequestUtils.getStringParameter(request, "corpId", "0");
			String funcParams = ServletRequestUtils.getStringParameter(request, "funcParams", "");
			fileUrl = ocrWebApiService.getFullFileDownloadUrl(fileUrl);
			//从后台设置中获取服务地址
			String servicePath = ConfSettingUtils.getConfSettingValue("ocrServerPathVal", corpId);
			String appToken = ConfSettingUtils.getConfSettingValue("ocrAppTokenVal", corpId);
			servicePath +="ofd/ocrImageToOfd";
			Map<String,String> params = new HashMap<String,String>();
			params.put("fileUrl", fileUrl);
			params.put("appToken", appToken);
			
			try {
				convertRs = ocrWebApiService.sendPost_new(servicePath, params,funcParams);
			} catch (HttpHostConnectException e) {
				e.printStackTrace();
				return OperationResult.buildFailureResult("转换异常");
			} catch (ParseException e) {
				e.printStackTrace();
				return OperationResult.buildFailureResult("转换异常");
			} catch (IOException e) {
				e.printStackTrace();
				return OperationResult.buildFailureResult("转换异常");
			}
			
			JSONObject urlJson = JSONObject.parseObject(convertRs);
			if(!"200".equals(urlJson.get("code"))) {
				return OperationResult.buildFailureResult(StringUtils.null2String(urlJson.get("error")));
			}
			return OperationResult.buildSuccessResult("成功",urlJson);
	}
	
	/**
	 * pdf转双层ofd（没有直接的文字结果，只有txt文件）
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getOcrPdfToOfd", method = RequestMethod.GET)
	@ResponseBody
	public OperationResult getOcrPdfToOfd(HttpServletRequest request) {
			String convertRs = "";
			String fileUrl = ServletRequestUtils.getStringParameter(request, "fileUrl", "");
			String corpId = ServletRequestUtils.getStringParameter(request, "corpId", "0");
			String funcParams = ServletRequestUtils.getStringParameter(request, "funcParams", "");
			fileUrl = ocrWebApiService.getFullFileDownloadUrl(fileUrl);
			//从后台设置中获取服务地址
			String servicePath = ConfSettingUtils.getConfSettingValue("ocrServerPathVal", corpId);
			String appToken = ConfSettingUtils.getConfSettingValue("ocrAppTokenVal", corpId);
			servicePath +="ofd/ocrPdfToOfd";
			Map<String,String> params = new HashMap<String,String>();
			params.put("fileUrl", fileUrl);
			params.put("appToken", appToken);
			
			try {
				convertRs = ocrWebApiService.sendPost_new(servicePath, params,funcParams);
			} catch (HttpHostConnectException e) {
				e.printStackTrace();
				return OperationResult.buildFailureResult("转换异常");
			} catch (ParseException e) {
				e.printStackTrace();
				return OperationResult.buildFailureResult("转换异常");
			} catch (IOException e) {
				e.printStackTrace();
				return OperationResult.buildFailureResult("转换异常");
			}
			
			JSONObject urlJson = JSONObject.parseObject(convertRs);
			if(!"200".equals(urlJson.get("code"))) {
				return OperationResult.buildFailureResult(StringUtils.null2String(urlJson.get("error")));
			}
			return OperationResult.buildSuccessResult("成功",urlJson);
	}
	
	/**
	 * pdf识别（转文字）
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getOcrPdf", method = RequestMethod.GET)
	@ResponseBody
	public OperationResult getOcrPdf(HttpServletRequest request) {
		String convertRs = "";
		String fileUrl = ServletRequestUtils.getStringParameter(request, "fileUrl", "");
		String corpId = ServletRequestUtils.getStringParameter(request, "corpId", "0");
		String funcParams = ServletRequestUtils.getStringParameter(request, "funcParams", "");
		fileUrl = ocrWebApiService.getFullFileDownloadUrl(fileUrl);
		//从后台设置中获取服务地址
		String servicePath = ConfSettingUtils.getConfSettingValue("ocrServerPathVal", corpId);
		String appToken = ConfSettingUtils.getConfSettingValue("ocrAppTokenVal", corpId);
		servicePath +="pdf/ocrPdf";
		Map<String,String> params = new HashMap<String,String>();
		params.put("fileUrl", fileUrl);
		//params.put("appToken", appToken);
		
		try {
			//convertRs = ocrWebApiService.sendPost(servicePath, params, funcParams);
			LOG.error("==========OCR识别传参servicePath:"+servicePath);
			LOG.error("==========OCR识别传参params1,fileUrl:"+fileUrl);
			LOG.error("==========OCR识别传参params2,appToken:"+appToken);
			LOG.error("==========OCR识别传参funcParams:"+funcParams);
			convertRs = ocrWebApiService.sendPost_new(servicePath, params, funcParams);
		} catch (HttpHostConnectException e) {
			e.printStackTrace();
			return OperationResult.buildFailureResult("转换异常");
		} catch (ParseException e) {
			e.printStackTrace();
			return OperationResult.buildFailureResult("转换异常");
		} catch (IOException e) {
			e.printStackTrace();
			return OperationResult.buildFailureResult("转换异常");
		}
		
		JSONObject urlJson = JSONObject.parseObject(convertRs);
		if(!"200".equals(urlJson.get("code"))) {
			return OperationResult.buildFailureResult(StringUtils.null2String(urlJson.get("error")));
		}
		return OperationResult.buildSuccessResult("成功",urlJson);
	}
	
	/**
	 * ofd识别（转文字）
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getOcrOfd", method = RequestMethod.GET)
	@ResponseBody
	public OperationResult getOcrOfd(HttpServletRequest request) {
		String convertRs = "";
		String fileUrl = ServletRequestUtils.getStringParameter(request, "fileUrl", "");
		String corpId = ServletRequestUtils.getStringParameter(request, "corpId", "0");
		String funcParams = ServletRequestUtils.getStringParameter(request, "funcParams", "");
		fileUrl = ocrWebApiService.getFullFileDownloadUrl(fileUrl);
		//从后台设置中获取服务地址
		String servicePath = ConfSettingUtils.getConfSettingValue("ocrServerPathVal", corpId);
		String appToken = ConfSettingUtils.getConfSettingValue("ocrAppTokenVal", corpId);
		servicePath +="ofd/ocrOfd";
		Map<String,String> params = new HashMap<String,String>();
		params.put("fileUrl", fileUrl);
		params.put("appToken", appToken);
		
		try {
			convertRs = ocrWebApiService.sendPost_new(servicePath, params, funcParams);
		} catch (HttpHostConnectException e) {
			e.printStackTrace();
			return OperationResult.buildFailureResult("转换异常");
		} catch (ParseException e) {
			e.printStackTrace();
			return OperationResult.buildFailureResult("转换异常");
		} catch (IOException e) {
			e.printStackTrace();
			return OperationResult.buildFailureResult("转换异常");
		}
		
		JSONObject urlJson = JSONObject.parseObject(convertRs);
		if(!"200".equals(urlJson.get("code"))) {
			return OperationResult.buildFailureResult(StringUtils.null2String(urlJson.get("error")));
		}
		return OperationResult.buildSuccessResult("成功",urlJson);
	}
}
